/*
 * Copyright (c) 2020-2099 the original author or authors. All rights reserve.
 */
package org.tianyun.cloud.amqp.rabbitmq.advice;

import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;

import java.lang.reflect.Method;

/**
 * 消费端拦截器
 *
 * @auther ebert_chan
 */
public interface ListenerInvokeAdvice extends MethodInterceptor {

    void onBefore(Method method, Object[] args) throws Throwable;

    void onAfter(Object value) throws Throwable;

    default Object invoke(MethodInvocation invocation) throws Throwable {
        Object[] args = invocation.getArguments();
        Method method = invocation.getMethod();
        Object value = null;
        try {
            this.onBefore(method, args);
            value = invocation.proceed();
        } finally {
            this.onAfter(value);
        }
        return value;
    }

}
